package ComputingSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class MainApp {
    static Scanner scanner=new Scanner(System.in);
    static ArrayList<User> users=new ArrayList<>();
    static ArrayList<MathTest> mathTests=new ArrayList<>();
    public static void main(String[] args) {
        mathTests.add(new AddTest(10,5));
        mathTests.add(new SubtractTest(6,3));
        mathTests.add(new MultiplyTest(10,2));
        mathTests.add(new DivideTest(8,2));
        users.add(new Student("哪吒","123456"));
        users.add(new Teacher("太乙真人","123456"));
        while (true){
            System.out.println("欢迎来到黑马数学心算练习系统");
            System.out.println("请输入用户名：");
            String name = scanner.next();
            for (User user2 : users) {
                if (user2.getName().equals(name)){
                    System.out.println("请输入用密码：");
                    String passWord = scanner.next();
                    if (user2.getPassword().equals(passWord)){
                        if (user2 instanceof Student){
                            showStudentUI();
                        }else {
                            showTeacherUI();
                        }
                    }
                }
            }
            System.out.println("继续登录系统请输入Y，否则退出系统");
            if (scanner.next().equals("Y")){
                continue;
            }else {
                break;
            }
        }

    }
    private static void addTest(){
        while (true){
            System.out.println("请选择需要添加的题型：");
            System.out.println("1:加法题");
            System.out.println("2:减法题");
            System.out.println("3:乘法题");
            System.out.println("4:除法题");
            int selection=scanner.nextInt();
            int a;
            int b;
            MathTest mathTest;
            switch (selection){
                case 1:
                    System.out.println("请输入被加数：");
                    a=scanner.nextInt();
                    System.out.println("请输入加数：");
                    b=scanner.nextInt();
                    mathTest=new AddTest(a,b);
                    mathTests.add(mathTest);
                    System.out.println("题目："+mathTest.getQuestion()+"添加成功！");
                    break;
                case 2:
                    System.out.println("请输入被减数：");
                    a=scanner.nextInt();
                    System.out.println("请输入减数：");
                    b=scanner.nextInt();
                    mathTest=new SubtractTest(a,b);
                    mathTests.add(mathTest);
                    System.out.println("题目："+mathTest.getQuestion()+"添加成功！");
                    break;
                case 3:
                    System.out.println("请输入被乘数：");
                    a=scanner.nextInt();
                    System.out.println("请输入乘数：");
                    b=scanner.nextInt();
                    mathTest=new MultiplyTest(a,b);
                    mathTests.add(mathTest);
                    System.out.println("题目："+mathTest.getQuestion()+"添加成功！");
                    break;
                case 4:
                    System.out.println("请输入被除数：");
                    a=scanner.nextInt();
                    System.out.println("请输入除数：");
                    b=scanner.nextInt();
                    mathTest=new DivideTest(a,b);
                    mathTests.add(mathTest);
                    System.out.println("题目："+mathTest.getQuestion()+"添加成功！");
                    break;

            }
            System.out.println("是否继续添加题目，是请按Y，否则任意输入");
            if (scanner.next().equals("Y")){
                continue;
            }else {
                break;
            }
        }

    }
    public int answer(){
        return 0;
    }
    private static void deleteTest(){
        PrintUtil.printMathTests(mathTests);
        int mTSize=mathTests.size();
        System.out.println("请问要删除哪一道题目：");
        String selection=scanner.next();
        for (int i = 0; i < mathTests.size(); i++) {
            if (selection.equals(mathTests.get(i).getQuestion())||selection.equals("问题"+(i+1))||selection.equals((i+1))){
                mathTests.remove(i);
            }
        }
        if (mTSize==mathTests.size()){
            System.out.println("没这道题");
        }

    }
    private static void showStudentUI(){
        while (true){
            System.out.println("欢迎哪吒同学进入系统！");
            System.out.println("请开始您的答题：");
            System.out.println("您好，一共"+mathTests.size()+"道题目，请开始作答，答对一题得1分，答错不给分");
            int myAnswer;
            int grade=0;
            for (int i = 0; i < mathTests.size(); i++) {
                System.out.println("请回答以下问题，机会只有一次：");
                System.out.println(mathTests.get(i).getQuestion());
                if (mathTests.get(i) instanceof DivideTest){
                    System.out.println("这是一道除法题，只要回答商就行，余数不考虑");
                }
                myAnswer=scanner.nextInt();
                if (myAnswer==mathTests.get(i).calculator()){
                    System.out.println("回答正确！");
                    grade++;
                }else {
                    System.out.println("回答错误。");
                }
            }
            System.out.println("一共"+mathTests.size()+"道题，每题一分，共得分"+grade);
            int mTSize=mathTests.size();
            if (mTSize==grade){
                System.out.println("哇哦！算术这方面，你就是王者！佩服你");
            }else if (grade>=(mTSize/2)){
                System.out.println("成绩还不错继续努力");
            }else {
                System.out.println("菜鸡回去种田吧");
            }
            System.out.println("成绩不服，输入Y继续挑战，否则输入其它退出！");
            if (scanner.next().equals("Y")){
                continue;
            }else{
                break;
            }
        }



    }
    private static void showTeacherUI(){

        while (true){
            System.out.println("欢迎太乙真人老师进入系统");
            System.out.println("当前题库如下：");
            PrintUtil.printMathTests(mathTests);
            System.out.println("是否要编辑题目,需要编辑输入Y,输入其他无需修改，并退出登录！");
            if (scanner.next().equals("Y")){
                System.out.println("1、添加题目");
                System.out.println("2、删除题目");
                int selection=scanner.nextInt();
                if (selection==1){
                    addTest();
                }else if (selection==2){
                    deleteTest();
                }
            }else {
                break;
            }
        }


    }

}